﻿<%@ WebHandler Language="C#" Class="control" %>

using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Diagnostics;


public class control : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        WebConnection webconnect = new WebConnection(context);
        MyLog mylog = new MyLog(context);

        string action = context.Request.QueryString["action"];
        if (action == "reboot")
        {
            ProcessStartInfo sti = new ProcessStartInfo();
            sti.FileName = context.Server.MapPath("reboot.bat");
            sti.Verb = "runas";
            Process.Start("shutdown","-r -t 0");
            context.Response.Write(sti.FileName + " " + sti.UserName);
        }

        if (action == "backup")
        {
            JToken conf = (JToken)JsonConvert.DeserializeObject(File.ReadAllText(context.Server.MapPath("Conf/FileSenderConf.json")));
            string port = conf["sendPort"].ToString();
            string res = webconnect.PostRequestJson("http://127.0.0.1:" + port + "/File/FileSender.ashx", "");
            mylog.writeLog(res);
            context.Response.Write(res);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}